﻿/******************************************************************************
 *  作者：       Maxzhang1985
 *  创建时间：   2012/5/2 11:13:15
 *
 *
 ******************************************************************************/
/******************************************************************************
 *  作者：       Maxzhang1985
 *  创建时间：   2012/5/2 10:23:18
 *
 *
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

using ExtendPropertyLib.WPF;
using ExtendPropertyLib;
namespace ExORMDesginer.Plugin
{
    [Serializable]
    public class DbProperty:BusinessInfoBase<DbProperty>
    {
        [NonSerialized]
        public static ExtendProperty NameProperty = RegisterProperty<DbProperty>(v => v.Name);
        [XmlAttribute("Name")]
        public string Name { set { SetValue(NameProperty, value); } get { return (string)GetValue(NameProperty); } }

        public static ExtendProperty DbNameProperty = RegisterProperty(v => v.DbName);
        /// <summary>
        /// 数据库中的名称
        /// </summary>
        [XmlAttribute("DbName")]
        public string DbName { set { SetValue(DbNameProperty, value); } get { return (string)GetValue(DbNameProperty); } }

        [NonSerialized]
        public static ExtendProperty DisplayNameProperty = RegisterProperty(v => v.DisplayName);
        [XmlIgnore]
        public string DisplayName { 
            set {
                SetValue(MemoProperty, value);
                SetValue(DisplayNameProperty, value); 
            } 
            get { 
                return string.IsNullOrEmpty(Memo) ? Name : Memo;
            } 
        }



    
        public static ExtendProperty MemoProperty = RegisterProperty<DbProperty>(v => v.Memo);
        [XmlAttribute("Memo")]
        public string Memo { set { SetValue(MemoProperty, value); } get { return (string)GetValue(MemoProperty); } }
   
        public static ExtendProperty IsPrimaryKeyProperty = RegisterProperty<DbProperty>(v => v.IsPrimaryKey);
        [XmlAttribute("IsPrimaryKey")]
        public bool IsPrimaryKey { set { SetValue(IsPrimaryKeyProperty, value); } get { return (bool)GetValue(IsPrimaryKeyProperty); } }


        public static ExtendProperty IsIdentityProperty = RegisterProperty<DbProperty>(v => v.IsIdentity);
        [XmlAttribute("IsIdentity")]
        public bool IsIdentity { set { SetValue(IsIdentityProperty, value); } get { return (bool)GetValue(IsIdentityProperty); } }

      
        public static ExtendProperty IdentitySpeedProperty = RegisterProperty<DbProperty>(v => v.IdentitySpeed);
        [XmlAttribute("IdentitySpeed")]
        public int IdentitySpeed { set { SetValue(IdentitySpeedProperty, value); } get { return (int)GetValue(IdentitySpeedProperty); } }

    
        public static ExtendProperty IdentitySetpProperty = RegisterProperty<DbProperty>(v => v.IdentitySetp);
        [XmlAttribute("IdentitySetp")]
        public int IdentitySetp { set { SetValue(IdentitySetpProperty, value); } get { return (int)GetValue(IdentitySetpProperty); } }


        public static ExtendProperty DataTypeProperty = RegisterProperty<DbProperty>(v => v.DataType);
        [XmlAttribute("DataType")]
        public string DataType { set { SetValue(DataTypeProperty, value); } get { return (string)GetValue(DataTypeProperty); } }


        public static ExtendProperty DataLengthProperty = RegisterProperty<DbProperty>(v => v.DataLength);
        [XmlAttribute("DataLength")]
        public int DataLength { set { SetValue(DataLengthProperty, value); } get { return (int)GetValue(DataLengthProperty); } }


        public static ExtendProperty DataDotLengthProperty = RegisterProperty<DbProperty>(v => v.DataDotLength);
        [XmlAttribute("DataDotLength")]
        public int DataDotLength { set { SetValue(DataDotLengthProperty, value); } get { return (int)GetValue(DataDotLengthProperty); } }


        public static ExtendProperty IsNullProperty = RegisterProperty<DbProperty>(v => v.IsNull);
        [XmlAttribute("IsNull")]
        public bool IsNull { set { SetValue(IsNullProperty, value); } get { return (bool)GetValue(IsNullProperty); } }


        public static ExtendProperty DefaultValueProperty = RegisterProperty<DbProperty>(v => v.DefaultValue);
        [XmlAttribute("DefaultValue")]
        public string DefaultValue { set { SetValue(DefaultValueProperty, value); } get { return (string)GetValue(DefaultValueProperty); } }


        public static ExtendProperty IsRemoveableProperty = RegisterProperty<DbProperty>(v => v.IsRemoveable);
        [XmlAttribute("IsRemoveable")]
        public bool IsRemoveable { set { SetValue(IsRemoveableProperty, value); } get { return (bool)GetValue(IsRemoveableProperty); } }

        
        public static ExtendProperty IsOutProperty = RegisterProperty(v => v.IsOut);
        /// <summary>
        /// 存储过程Out参数
        /// </summary>
        public bool IsOut { set { SetValue(IsOutProperty, value); } get { return (bool)GetValue(IsOutProperty); } }    
      
        [XmlIgnore]
        
        public Type PropertyType { set; get; }
        [XmlAttribute("PropertyType")]
        public string PropertyTypeName { set; get; }

        public string PropertyName
        {
            get
            {
                return this.Name + "Property";

            }
        }

        public string GetAttribute()
        {
            StringBuilder attr = new StringBuilder();
            attr.Append("[Column(Name=\"").Append(this.Name).Append("\"");
            if (this.IsPrimaryKey)
                attr.Append(",IsPrimaryKey=true");
            if(this.IsNull)
                attr.Append(",IsNullable=true");
            if(this.IsIdentity)
                attr.Append(",IsDbGenerated=true");
            attr.Append(")]");
            return attr.ToString();
        }

    }
}
